package com.cykj.user.controller;

import com.cykj.common.core.utils.StringUtils;
import com.cykj.common.core.web.controller.BaseController;
import com.cykj.common.core.web.domain.AjaxResult;
import com.cykj.system.api.domain.CtAddress;
import com.cykj.user.service.IAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 用户地址控制器
 */
@RestController
@RequestMapping("/address")
public class AddressController extends BaseController {

    @Autowired
    private IAddressService addressService;

    /**
     * 获取地址列表
     */
    @GetMapping("/list")
    public AjaxResult list(Long customerId) {
        List<CtAddress> list = addressService.selectAddressList(customerId);
        return AjaxResult.success(list);
    }

    /**
     * 获取地址详情
     */
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return AjaxResult.success(addressService.selectAddressById(id));
    }

    /**
     * 新增地址
     */
    @PostMapping
    public AjaxResult add(@RequestBody CtAddress address) {
        if (addressService.insertAddress(address) > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error("新增地址失败");
    }

    /**
     * 修改地址
     */
    @PutMapping
    public AjaxResult edit(@RequestBody CtAddress address) {
        if (addressService.updateAddress(address) > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error("修改地址失败");
    }

    /**
     * 删除地址
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        if (addressService.deleteAddressById(id) > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error("删除地址失败");
    }

    /**
     * 设置默认地址
     */
    @PutMapping("/default")
    public AjaxResult setDefault(@RequestBody CtAddress address) {
        if (addressService.setDefaultAddress(address.getId(), address.getCustomerId()) > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error("设置默认地址失败");
    }

    /**
     * 逆地理编码
     */
    @GetMapping("/reverse-geocode")
    public AjaxResult reverseGeocode(Double longitude, Double latitude) {
        if (longitude == null || latitude == null) {
            return AjaxResult.error("参数错误");
        }
        return AjaxResult.success(addressService.reverseGeocode(longitude, latitude));
    }

    /**
     * 地址转经纬度（新增接口）
     */
    @GetMapping("/geocode")
    public AjaxResult geocode(String address) {
        if (StringUtils.isEmpty(address)) {
            return AjaxResult.error("地址不能为空");
        }
        return AjaxResult.success(addressService.geocode(address));
    }

    /**
     * 搜索地址（扩展支持周边检索）
     */
    @GetMapping("/search")
    public AjaxResult search(
            String keyword,
            String region,
            Double longitude,  // 新增：经度
            Double latitude,   // 新增：纬度
            Integer radius) {  // 新增：检索半径（米）
        if (StringUtils.isEmpty(keyword)) {
            return AjaxResult.error("请输入搜索关键词");
        }
        // 调用扩展后的搜索方法，支持周边检索
        List<Map<String, Object>> results = addressService.searchAddress(
                keyword, region, longitude, latitude, radius);
        return AjaxResult.success(results);
    }
}